package engine

import (
	"my-crawler/config"
	"time"
)

//部分选项模式的结构体
type ReqData struct {
	Url     string
	Header  map[string]string
	Payload map[string]string
	Rate    <-chan time.Time
}
type Ops func(data *ReqData)

//因为还有之前的代码用这个
func CreateReqData(header, payload map[string]string, url string, ops ...Ops) ReqData {
	conf := config.LoadConfig()
	var rateLimiter = time.Tick(
		time.Millisecond * time.Duration(conf.App.QPS))

	data := &ReqData{
		Url:     url,
		Header:  header,
		Payload: payload,
		Rate:    rateLimiter,
	}
	for _, o := range ops {
		o(data)
	}
	return *data
}

//添加定时器
func WithRate(rate <-chan time.Time) Ops {
	return func(data *ReqData) {
		data.Rate = rate
	}
}
